In [1]:
import numpy as np
!python setup_cy.py build_ext --inplace

#The following codes for cython testing
import jpy
import jcy

reload( jpy)
#In cython, reload is not supported because it is related to kernal functions. 
reload( jcy)


Compiling jcy.pyx because it changed.
Cythonizing jcy.pyx
running build_ext
building 'jcy' extension
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c jcy.c -o build/temp.linux-x86_64-2.7/jcy.o
In file included from /usr/include/python2.7/numpy/ndarraytypes.h:1761:0,
                 from /usr/include/python2.7/numpy/ndarrayobject.h:17,
                 from /usr/include/python2.7/numpy/arrayobject.h:4,
                 from jcy.c:258:
/usr/include/python2.7/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
 #warning "Using deprecated NumPy API, disable it by " \
  ^
In file included from /usr/include/python2.7/numpy/ndarrayobject.h:26:0,
                 from /usr/include/python2.7/numpy/arrayobject.h:4,
                 from jcy.c:258:
/usr/include/python2.7/numpy/__multiarray_api.h:1629:1: warning: ‘_import_array’ defined but not used [-Wunused-function]
 _import_array(void)
 ^
In file included from /usr/include/python2.7/numpy/ufuncobject.h:327:0,
                 from jcy.c:259:
/usr/include/python2.7/numpy/__ufunc_api.h:241:1: warning: ‘_import_umath’ defined but not used [-Wunused-function]
 _import_umath(void)
 ^
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/jcy.o -o /home/jamessungjinkim/Dropbox/Aspuru-Guzik/python_lab/jamespy/jcy.so
Out[1]:
<module 'jcy' from 'jcy.so'>

Numpy testing


In [2]:
A = [2,2,3]
jcy.f_test(A)
print A


[1, 2, 3]

In [3]:
C = np.array([0,0], dtype = np.float64)
A = np.array([1,2], dtype = np.float64)
B = np.array([3,5], dtype = np.float64)

In [5]:
%timeit jcy.jsum_float( C, A, B, 1000)
print C


The slowest run took 4.02 times longer than the fastest. This could mean that an intermediate result is being cached 
100000 loops, best of 3: 4.93 µs per loop
[ 4.  7.]

In [6]:
%timeit jpy.jsum_float( C, A, B, 1000)
print C


1000 loops, best of 3: 568 µs per loop
[ 4.  7.]

jsum testing

  • jsum is very basic function for testing cython. It should be mcuh faster in cython than in python.

In [20]:
%timeit jcy.jsum( 100)
%timeit jpy.jsum( 100)
96.2*1e3/404


1000 loops, best of 3: 394 µs per loop
10 loops, best of 3: 97 ms per loop
Out[20]:
238.11881188118812

In [11]:
C = np.array([0,0])
A = np.array([1,2])
B = np.array([3,5])
print A, B, C


[1 2] [3 5] [0 0]

In [8]:
jpy.jsum_float( C, A, B)
print C


[0 0]

In [4]:
def f(A):
    A[0] = 1


f(A)
print A


[1, 2, 3]

In [9]:
def f(A):
    A[0] = 1

A = np.array([2,2,3])
f(A)
print A


[1 2 3]

In [8]:
jpy.f(A)
print A


[1, 2, 3]

In [ ]: